﻿/*
    This software is licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

    Copyright (C) 2010 Daniel Deptford (redmercury@gmail.com)
 
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COLLADASharp.Core
{
    using LibraryForceFields = COLLADASharp.Physics.LibraryForceFields;
    using LibraryPhysicsMaterials = COLLADASharp.Physics.LibraryPhysicsMaterials;
    using LibraryPhysicsModels = COLLADASharp.Physics.LibraryPhysicsModels;
    using LibraryPhysicsScenes = COLLADASharp.Physics.LibraryPhysicsScenes;
    using LibraryEffects = COLLADASharp.FX.LibraryEffects;
    using LibraryMaterials = COLLADASharp.FX.LibraryMaterials;
    using LibraryImages = COLLADASharp.FX.LibraryImages;

    [DAEClass("COLLADA")]
    public class COLLADA : DAENode
    {
        public COLLADA() { }

        static COLLADA()
        {
            NodeProperty.InitializeForClass(typeof(COLLADA));
        }

        public override COLLADA Document
        {
            get
            {
                return this;
            }
            set
            {
                throw new NotSupportedException("COLLADA object cannot have its document set");
            }
        }

        private Dictionary<string, IDDAENode> registeredNodes = new Dictionary<string, IDDAENode>();

        internal void RegisterIDNode(IDDAENode node)
        {
            string id = node.ID;
            if (id == null)
                throw new ArgumentException("Node does not have an id assigned to it");

            if (registeredNodes.ContainsKey(id))
                throw new Exception(string.Format("Document already contains a node with the id", id));

            registeredNodes.Add(id, node);
        }

        internal void UnregisterIDNode(IDDAENode node)
        {
            string id = node.ID;
            if (id == null)
                throw new ArgumentException("Node does not have an id assigned to it");

            if (!registeredNodes.ContainsKey(id))
                throw new Exception(string.Format("Document does not contain a node with the id", id));

            registeredNodes.Remove(id);
        }

        [DAEElement(typeof(Asset), 0)]
        [DAENodeProperty("Asset", typeof(Asset))]
        private static NodeProperty assetProperty;
        public Asset Asset
        {
            get {return GetValue(assetProperty) as Asset;} 
            set {SetValue(assetProperty, value);} 
        }


        [DAEElement(typeof(LibraryAnimations), 1)]
        [DAEElement(typeof(LibraryAnimationClips), 1)]
        [DAEElement(typeof(LibraryCameras), 1)]
        [DAEElement(typeof(LibraryControllers), 1)]
        [DAEElement(typeof(LibraryEffects), 1)]
        [DAEElement(typeof(LibraryForceFields), 1)]
        [DAEElement(typeof(LibraryGeometries), 1)]
        [DAEElement(typeof(LibraryImages), 1)]
        [DAEElement(typeof(LibraryLights), 1)]
        [DAEElement(typeof(LibraryMaterials), 1)]
        [DAEElement(typeof(LibraryNodes), 1)]
        [DAEElement(typeof(LibraryPhysicsMaterials), 1)]
        [DAEElement(typeof(LibraryPhysicsModels), 1)]
        [DAEElement(typeof(LibraryPhysicsScenes), 1)]
        [DAEElement(typeof(LibraryVisualScenes), 1)]
        [DAENodeProperty("Libraries", typeof(DAENodeList<Library>))]
        private static NodeProperty libraryProperty;
        public DAENodeList<Library> Libraries
        {
            get { return GetValue(libraryProperty) as DAENodeList<Library>; }
            set { SetValue(libraryProperty, value); }
        }

        [DAEElement(typeof(Scene), 2)]
        [DAENodeProperty("Scene", typeof(Scene))]
        private static NodeProperty sceneProperty;
        public Scene Scene
        {
            get {return GetValue(sceneProperty) as Scene;} 
            set {SetValue(sceneProperty, value);} 
        }

        [DAEElement(typeof(Extra), 3)]
        [DAENodeProperty("Extra", typeof(Extra))]
        private static NodeProperty extraProperty;
        public Extra Extra
        {
            get {return GetValue(extraProperty) as Extra;} 
            set {SetValue(extraProperty, value);} 
        }
    }

    [DAEClass("contributor")]
    public class Contributor : DAENode
    {
        [DAEElement("author", typeof(string), 0)]
        [DAENodeProperty("Author", typeof(string))]
        private static NodeProperty authorProperty;
        public string Author
        {
            get {return GetValue(authorProperty) as string;}
            set {SetValue(authorProperty, value);}
        }

        [DAEElement("authoring_tool", typeof(string), 1)]
        [DAENodeProperty("AuthoringTool", typeof(string))]
        private static NodeProperty authoringToolProperty;
        public string AuthoringTool
        {
            get {return GetValue(authoringToolProperty) as string;}
            set {SetValue(authoringToolProperty, value);}
        }

        [DAEElement("comments", typeof(string), 2)]
        [DAENodeProperty("Comments", typeof(string))]
        private static NodeProperty commentsProperty;
        public string Comments
        {
            get {return GetValue(commentsProperty) as string;}
            set {SetValue(commentsProperty, value);}
        }

        [DAEElement("copyright", typeof(string), 3)]
        [DAENodeProperty("Copyright", typeof(string))]
        private static NodeProperty copyrightProperty;
        public string Copyright
        {
            get {return GetValue(copyrightProperty) as string;}
            set {SetValue(copyrightProperty, value);}
        }

        [DAEElement("source_data", typeof(string), 4)]
        [DAENodeProperty("SourceData", typeof(string))]
        private static NodeProperty sourceDataProperty;
        public string SourceData
        {
            get { return GetValue(sourceDataProperty) as string; }
            set { SetValue(sourceDataProperty, value); }
        }
    }

    [DAEClass("asset")]
    public class Asset : DAENode
    {
        [DAEElement(typeof(Contributor), 0)]
        [DAENodeProperty("Contributors", typeof(DAENodeList<Contributor>))]
        private static NodeProperty contributorsProperty;
        public DAENodeList<Contributor> Contributors
        {
            get { return GetValue(contributorsProperty) as DAENodeList<Contributor>; }
            set {SetValue(contributorsProperty, value);}
        }

        [DAEElement("created", typeof(DateTime), 1)]
        [DAENodeProperty("Created", typeof(DateTime))]
        private static NodeProperty createdProperty;
        public DateTime Created
        {
            get {return (DateTime)GetValue(createdProperty);}
            set {SetValue(createdProperty, value);}
        }

        [DAEElement("keywords", typeof(string), 2)]
        [DAENodeProperty("Keywords", typeof(string))]
        private static NodeProperty keywordsProperty;
        public string Keywords
        {
            get {return GetValue(keywordsProperty) as string;}
            set {SetValue(keywordsProperty, value);}
        }

        [DAEElement("modified", typeof(DateTime), 3)]
        [DAENodeProperty("Modified", typeof(DateTime))]
        private static NodeProperty modifiedProperty;
        public DateTime Modified
        {
            get {return (DateTime)GetValue(modifiedProperty);}
            set {SetValue(modifiedProperty, value);}
        }

        [DAEElement("revision", typeof(string), 4)]
        [DAENodeProperty("Revision", typeof(string))]
        private static NodeProperty revisionProperty;
        public string Revision
        {
            get {return GetValue(revisionProperty) as string;}
            set {SetValue(revisionProperty, value);}
        }

        [DAEElement("subject", typeof(string), 5)]
        [DAENodeProperty("Subject", typeof(string))]
        private static NodeProperty subjectProperty;
        public string Subject
        {
            get {return GetValue(subjectProperty) as string;}
            set {SetValue(subjectProperty, value);}
        }

        [DAEElement("title", typeof(string), 6)]
        [DAENodeProperty("Title", typeof(string))]
        private static NodeProperty titleProperty;
        public string Title
        {
            get {return GetValue(titleProperty) as string;}
            set {SetValue(titleProperty, value);}
        }


        [DAEClass("unit")]
        public class SizeUnit : DAENode
        {
            [DAEAttribute("meter")]
            [DAENodeProperty("Meter", typeof(float))]
            private static NodeProperty meterProperty;
            public float Meter
            {
                get { return (float)GetValue(meterProperty);}
                set {SetValue(meterProperty, value);}
            }

            [DAEAttribute("name")]
            [DAENodeProperty("Name", typeof(string))]
            private static NodeProperty nameProperty;
            public string Name
            {
                get {return GetValue(nameProperty) as string;}
                set {SetValue(nameProperty, value);}
            }
        }

        [DAEElement(typeof(SizeUnit), 7)]
        [DAENodeProperty("Unit", typeof(SizeUnit))]
        private static NodeProperty unitProperty;
        public SizeUnit Unit
        {
            get {return GetValue(unitProperty) as SizeUnit;}
            set {SetValue(unitProperty, value);}
        }

        public enum Axis
        {
            X_UP,
            Y_UP,
            Z_UP
        }

        [DAEElement("up_axis",typeof(Axis), 8)]
        [DAENodeProperty("UpAxis", typeof(Axis))]
        private static NodeProperty upAxisProperty = NodeProperty.Register("UpAxis", typeof(Axis), typeof(Asset), Axis.Y_UP);
        public Axis UpAxis
        {
            get {return (Axis)GetValue(upAxisProperty);}
            set {SetValue(upAxisProperty, value);}
        }
    }

    [DAEClass("extra")]
    public class Extra : DAENode
    {
    
    }

    public abstract class Library : IDDAENode
    {
        [DAEAttribute("name")]
        private static NodeProperty nameProperty = NodeProperty.Register("Name", typeof(string), typeof(Library));
        public string Name
        {
            get { return GetValue(nameProperty) as string; }
            set { SetValue(nameProperty, value); }
        }
    }

    [DAEClass("URL")]
    public class Url
    {
        
    }
}
